﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text.RegularExpressions;

/// <summary>
///BBS_Utils 的摘要说明
/// </summary>
public class BBS_Utils
{
    /// <summary>
    /// 从字符串的指定位置截取指定长度的子字符串
    /// </summary>
    /// <param name="str">原字符串</param>
    /// <param name="startIndex">子字符串的起始位置</param>
    /// <param name="length">子字符串的长度</param>
    /// <returns>子字符串</returns>
    public static string CutString(string str, int startIndex, int length)
    {
        if (startIndex >= 0)
        {
            if (length < 0)
            {
                length = length * -1;
                if (startIndex - length < 0)
                {
                    length = startIndex;
                    startIndex = 0;
                }
                else
                    startIndex = startIndex - length;
            }

            if (startIndex > str.Length)
                return "";
        }
        else
        {
            if (length < 0)
                return "";
            else
            {
                if (length + startIndex > 0)
                {
                    length = length + startIndex;
                    startIndex = 0;
                }
                else
                    return "";
            }
        }

        if (str.Length - startIndex < length)
            length = str.Length - startIndex;

        return str.Substring(startIndex, length);
    }

    /// <summary>
    /// 分割字符串
    /// </summary>
    public static string[] SplitString(string strContent, string strSplit)
    {
        if (!StrIsNullOrEmpty(strContent))
        {
            if (strContent.IndexOf(strSplit) < 0)
                return new string[] { strContent };

            return Regex.Split(strContent, Regex.Escape(strSplit), RegexOptions.IgnoreCase);
        }
        else
            return new string[0] { };
    }

    /// <summary>
    /// 字段串是否为Null或为""(空)
    /// </summary>
    /// <param name="str"></param>
    /// <returns></returns>
    public static bool StrIsNullOrEmpty(string str)
    {
        if (str == null || str.Trim() == string.Empty)
            return true;

        return false;
    }

    /// <summary>
    /// 判断指定字符串是否属于指定字符串数组中的一个元素
    /// </summary>
    /// <param name="strSearch">字符串</param>
    /// <param name="stringArray">字符串数组</param>
    /// <param name="caseInsensetive">是否不区分大小写, true为不区分, false为区分</param>
    /// <returns>判断结果</returns>
    public static bool InArray(string strSearch, string[] stringArray, bool caseInsensetive)
    {
        return GetInArrayID(strSearch, stringArray, caseInsensetive) >= 0;
    }

    /// <summary>
    /// 判断指定字符串是否属于指定字符串数组中的一个元素
    /// </summary>
    /// <param name="str">字符串</param>
    /// <param name="stringarray">字符串数组</param>
    /// <returns>判断结果</returns>
    public static bool InArray(string str, string[] stringarray)
    {
        return InArray(str, stringarray, false);
    }

    /// <summary>
    /// 判断指定字符串在指定字符串数组中的位置
    /// </summary>
    /// <param name="strSearch">字符串</param>
    /// <param name="stringArray">字符串数组</param>
    /// <param name="caseInsensetive">是否不区分大小写, true为不区分, false为区分</param>
    /// <returns>字符串在指定字符串数组中的位置, 如不存在则返回-1</returns>
    public static int GetInArrayID(string strSearch, string[] stringArray, bool caseInsensetive)
    {
        for (int i = 0; i < stringArray.Length; i++)
        {
            if (caseInsensetive)
            {
                if (strSearch.ToLower() == stringArray[i].ToLower())
                    return i;
            }
            else if (strSearch == stringArray[i])
                return i;
        }
        return -1;
    }


    /// <summary>
    /// 判断指定字符串在指定字符串数组中的位置
    /// </summary>
    /// <param name="strSearch">字符串</param>
    /// <param name="stringArray">字符串数组</param>
    /// <returns>字符串在指定字符串数组中的位置, 如不存在则返回-1</returns>		
    public static int GetInArrayID(string strSearch, string[] stringArray)
    {
        return GetInArrayID(strSearch, stringArray, true);
    }

    /// <summary>
    /// 判断指定字符串是否属于指定字符串数组中的一个元素
    /// </summary>
    /// <param name="str">字符串</param>
    /// <param name="stringarray">内部以逗号分割单词的字符串</param>
    /// <returns>判断结果</returns>
    public static bool InArray(string str, string stringarray)
    {
        return InArray(str, SplitString(stringarray, ","), false);
    }

    /// <summary>
    /// 将对象转换为Int32类型
    /// </summary>
    /// <param name="expression">要转换的字符串</param>
    /// <param name="defValue">缺省值</param>
    /// <returns>转换后的int类型结果</returns>
    public static int StrToInt(object expression, int defValue)
    {
        return BBS_TypeConverter.ObjectToInt(expression, defValue);
    }

}